package com.twotiger.and.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.bean.Invester;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.DateUtil;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.StringUtils;

/**
 * 参与人页面的adapter Created by victor on 2014/11/7 0007.
 */
public class InvesterAdapter extends BaseAdapter {
    private Activity mactivity;
    private ArrayList<Invester> mList;

    public InvesterAdapter(Activity _Activity, ArrayList<Invester> _List) {
        this.mactivity = _Activity;
        this.mList = _List;
    }

    public void resetData(ArrayList<Invester> _List) {
        if (!ListUtils.isEmpty(_List)) {
            mList.addAll(_List);
        }
        notifyDataSetChanged();
    }

    public void sortData(int sort) {
        switch (sort) {
        case 0:
            Collections.sort(mList, new Comparator<Invester>() {
                @Override
                public int compare(Invester lhs, Invester rhs) {// 时间
                    long lhs_Ctime = lhs.getCtime();
                    long rhs_Ctime = rhs.getCtime();
                    if (lhs_Ctime - rhs_Ctime > 0) {
                        return 1;
                    } else {
                        return -1;
                    }
                }
            });
            break;
        case 1:
            Collections.sort(mList, new Comparator<Invester>() { // 钱
                    @Override
                    public int compare(Invester lhs, Invester rhs) {
                        String lhs_amount = lhs.getAmount();
                        String rhs_amount = rhs.getAmount();
                        if (ArithUtils.compareTo(lhs_amount, rhs_amount) > 0) {
                            return -1;
                        } else {
                            return 1;
                        }
                    }
                });
            break;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (!ListUtils.isEmpty(mList)) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Invester getItem(int position) {
        if (!ListUtils.isEmpty(mList) && position < mList.size()) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mactivity, R.layout.invester_list_item, null);
        }
        TextView invester_name_tv = ViewHolder.get(convertView, R.id.invester_name_tv);
        TextView invester_date_tv = ViewHolder.get(convertView, R.id.invester_date_tv);
        TextView invester_money_tv = ViewHolder.get(convertView, R.id.invester_money_tv);
        Invester invester = getItem(position);
        if (invester != null) {
            invester_name_tv.setText(String.valueOf(StringUtils.hideMidString(invester
                .getUserName())));
            invester_date_tv.setText(String.valueOf(DateUtil.getDateStringForLong(
                invester.getCtime(), DateUtil.FMT)));
            invester_money_tv.setText(String.valueOf(invester.getAmount()));
        } else {
            return null;
        }
        return convertView;
    }
}
